package com.itheima.reggie.service.impl;

import cn.hutool.Hutool;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Transactional
    @Override
    public R saveSetmeal(SetmealDto setmealDto) {
        //将setmeal中需要的数据新增入setmeal表中
        this.save(setmealDto);

        //将封装的dish数据插入setmeal_dish表中
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
        for (SetmealDish dish : dishes) {
            dish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(dishes);
        return R.success("新增套餐成功");
    }

    @Transactional
    @Override
    public R deleteSetmeal(List<Long> ids) {

        //查询套餐的状态是否停售
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId, ids);
        qw.eq(Setmeal::getStatus, 1);
        int count = this.count(qw);
        if (count > 0) {
            return R.error("当前套餐正在售卖中,无法删除");
        }
        //套餐停售,删除套餐
        this.removeByIds(ids);

        //删除套餐菜品表中的对应得菜品
        LambdaQueryWrapper<SetmealDish> qw2 = new LambdaQueryWrapper<>();
        qw2.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(qw2);

        return R.success("删除套餐成功");
    }

    @Override
    public R<SetmealDto> getSetmeal(Long id) {

        Setmeal setmeal = this.getById(id);

        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);

        List<SetmealDish> list = setmealDishService.list(qw);

        SetmealDto setmealDto = new SetmealDto();

        BeanUtils.copyProperties(setmeal,setmealDto);

        setmealDto.setCategoryName(null);

        setmealDto.setSetmealDishes(list);

        return R.success(setmealDto);
    }

    @Override
    public R updateSetmeal(SetmealDto setmealDto) {
        this.updateById(setmealDto);

        //删除之前套菜中关联的菜品
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(qw);

        //
        List<SetmealDish> dishList = setmealDto.getSetmealDishes();

        if (dishList!=null && dishList.size()>0){

            for (SetmealDish setmealDish : dishList) {
                setmealDish.setSetmealId(setmealDto.getId());
            }
            setmealDishService.saveBatch(dishList);
        }

        return R.success("修改套餐成功");
    }


}
